package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ydkj.a37e_mall.Classes.Applicatiom.MyApplication;
import com.ydkj.a37e_mall.Classes.Model.Request.GetLocationReq;
import com.ydkj.a37e_mall.Classes.Model.Response.GetAddressCityResp;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_get;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.GsonImpl;
import com.ydkj.a37e_mall.Resource.Utils.Rsa;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;
import com.ydkj.a37e_mall.Resource.View.StatusBarUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * Created by hong on 2016/3/10.
 * <p>
 * 选择街道
 */
public class ChooseAddressLocation extends BaseActivity {

    private Activity mActivity;
    private MyApplication myApplication;
    private ChooseLocationAdapter chooseLocationAdapter;
    private List<GetAddressCityResp.ResultEntity.DataEntity> dataEntityList;
    private ListView lv_address;
    private ImageView img_back;
    private int code;
    private int province_code, city_code, area_code;
    private String province_name, city_name, area_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_address);
        mActivity = this;
        init();
//        initWindow();
    }

    private void initWindow() {
        StatusBarUtil.setColor(mActivity, getResources().getColor(R.color.A333333), 0);
    }

    private void init() {
        myApplication = MyApplication.getInstance();

        if (this.getIntent().getExtras() != null) {
            code = this.getIntent().getExtras().getInt("code");
            province_code = this.getIntent().getExtras().getInt("province_code");
            city_code = this.getIntent().getExtras().getInt("city_code");
            area_code = this.getIntent().getExtras().getInt("area_code");
            province_name = this.getIntent().getExtras().getString("province_name");
            city_name = this.getIntent().getExtras().getString("city_name");
            area_name = this.getIntent().getExtras().getString("area_name");
            Log.e("url", "code==" + code);
        }
        GetLocation();

        img_back = (ImageView) mActivity.findViewById(R.id.img_back);
        lv_address = (ListView) mActivity.findViewById(R.id.lv_address);
        dataEntityList = new ArrayList<GetAddressCityResp.ResultEntity.DataEntity>();
        chooseLocationAdapter = new ChooseLocationAdapter(dataEntityList);
        lv_address.setAdapter(chooseLocationAdapter);

        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private class ChooseLocationAdapter extends BaseAdapter {
        private List<GetAddressCityResp.ResultEntity.DataEntity> dataEntityList;

        private ChooseLocationAdapter(List<GetAddressCityResp.ResultEntity.DataEntity> infos) {
            if (infos == null) {
                infos = new ArrayList<GetAddressCityResp.ResultEntity.DataEntity>();
            }
            dataEntityList = infos;
        }

        @Override
        public int getCount() {
            return dataEntityList.size();
        }

        /**
         * 获得某一位置的数据
         */
        @Override
        public GetAddressCityResp.ResultEntity.DataEntity getItem(int position) {
            return dataEntityList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder vh = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.choose_address_listitem, null);
                vh = new ViewHolder();
                vh.tv_address = (TextView) convertView.findViewById(R.id.bankname);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            convertView.setTag(vh);
            convertView.setClickable(true);

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (myApplication.getIntent_code() == 1) {
                        Intent intent = new Intent(ChooseAddressLocation.this, AddNewAddress.class);
                        myApplication.setLocation_id1(getItem(position).getId() + "");
                        myApplication.setLocation1(getItem(position).getName());
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        finish();
                    }
                    if (myApplication.getIntent_code() == 2) {
                        Intent intent = new Intent(ChooseAddressLocation.this, EditAddress.class);
                        myApplication.setLocation_id(getItem(position).getId() + "");
                        myApplication.setLocation(getItem(position).getName());
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        Log.e("url", "intent==" + province_name + city_name + area_name + getItem(position).getName());
                        startActivity(intent);
                        finish();
                    }
                    //申请门店
                    if (myApplication.getIntent_code() == 4) {
                        Intent intent = new Intent(ChooseAddressLocation.this, ApplyBusinessActivity.class);
                        myApplication.setLocation_id3(getItem(position).getId() + "");
                        myApplication.setLocation3(getItem(position).getName());
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        startActivity(intent);
                        finish();

                    }
                    //门店管理
                    if (myApplication.getIntent_code() == 5) {
                        Intent intent = new Intent(ChooseAddressLocation.this, StoreManagementActivity.class);
                        myApplication.setLocation_id3(getItem(position).getId() + "");
                        myApplication.setLocation3(getItem(position).getName());
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        startActivity(intent);
                        finish();

                    }
                }
            });

            vh.tv_address.setText(getItem(position).getName());

            return convertView;
        }

        private class ViewHolder {
            public TextView tv_address;
        }
    }

    //获取街道地址
    private void GetLocation() {
        CustomProgressDialog.createDialog(mActivity).Show();
        GetLocationReq req = new GetLocationReq();
        req.setType("location");
        if (myApplication.getIntent_code() == 1) {
            req.setParent(myApplication.getArea_code1());
            Log.e("url", "setParent1==" + req.getParent());
        }
        if (myApplication.getIntent_code() == 2) {
            req.setParent(myApplication.getArea_code());
            Log.e("url", "setParent2==" + req.getParent());
        }
        if (myApplication.getIntent_code() == 4) {
            req.setParent(myApplication.getArea_code3());
        }
        if (myApplication.getIntent_code() == 5) {
            req.setParent(myApplication.getArea_code3());
        }
        String url = Const.COMMON_ADDRESS + req.urlString();
        RequestParams params = new RequestParams();
        try {
            String source = req.getString();
            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(source);
        } catch (Exception e) {
            e.printStackTrace();
        }


        params.put("secret", secret);
        Async_http_get.get(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                Log.e("url", "response==" + response);
                if (errno.equals("000")) {
                    GetAddressCityResp resp = GsonImpl.get().toObject(response.toString(), GetAddressCityResp.class);
                    //把数据填入Resp后，再把数据填入list里
                    dataEntityList.addAll(resp.getResult().getData());
                    chooseLocationAdapter.notifyDataSetChanged();
                }
                if (!errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接异常");

            }
        });
    }
}
